package com.tcoiss.datafactory.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcoiss.common.core.domain.R;
import com.tcoiss.common.core.utils.DateUtils;
import com.tcoiss.common.component.api.RemoteApiService;
import com.tcoiss.common.component.api.model.ApiParam;
import com.tcoiss.datafactory.domain.KJob;
import com.tcoiss.datafactory.mapper.KJobMapper;
import com.tcoiss.datafactory.service.IKJobService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作业Service业务层处理
 *
 * @author zw
 * @date 2021-06-01
 */
@Service
public class KJobServiceImpl extends ServiceImpl<KJobMapper, KJob> implements IKJobService {

    @Resource
    private RemoteApiService remoteApiService;

    @Override
    public List<KJob> queryList(KJob kJob) {
        List<KJob> kJobList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        ApiParam apiUrl = new ApiParam();
        map.put("size",1000);
        apiUrl.setParam(map);
        apiUrl.setApiCode("ETLJobQuery");
        R<Map<String, Object>> response = remoteApiService.executeApi(apiUrl);
        if(response.getCode() == 200){
            Map<String, Object> mapData = response.getData();
            if ((Boolean) mapData.get("success") == true) {
                Map<String, Object> resultMap = (Map<String, Object>) mapData.get("result");
                if((Integer) resultMap.get("size") <= 0){
                    return kJobList;
                }
                List<Map<String,Object>> contentListMap = (List<Map<String, Object>>) resultMap.get("content");
                for(int i = 0; i < contentListMap.size(); i++){
                    Map<String, Object> jobMap = contentListMap.get(i);
                    KJob job = new KJob();
                    job.setId(Long.valueOf((Integer) jobMap.get("id")));
                    job.setJobName((String) jobMap.get("jobName"));
                    job.setJobDescription((String) jobMap.get("jobDescription"));
                    job.setJobType((String) jobMap.get("jobType"));
                    job.setJobPath((String) jobMap.get("jobPath"));
                    job.setJobRepositoryId(Long.valueOf((Integer) jobMap.get("jobRepositoryId")));
                    job.setJobStatus(Long.valueOf((Integer) jobMap.get("jobStatus")));
                    job.setAddTime(DateUtils.strParseToDate(jobMap.get("addTime")));
                    job.setDelFlag(Long.valueOf((Integer) jobMap.get("delFlag")));
                    job.setCategoryId(Long.valueOf((Integer) jobMap.get("categoryId")));
                    kJobList.add(job);
                }
                return kJobList;
            }
        }
        return null;
    }

    /** 单个启动ETL作业 */
    @Override
    public Map<String, Object> startJobById(Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        ApiParam apiUrl = new ApiParam();
        apiUrl.setParam(map);
        apiUrl.setApiCode("ETLStartJob");
        R<Map<String, Object>> response = remoteApiService.executeApi(apiUrl);
        Map<String, Object> mapData = response.getData();
        return mapData;
    }
}
